package com.example.dw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dw.entity.User;
import com.example.dw.vo.UserVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户Mapper接口
 * @author system
 * @since 2024-01-01
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据用户名查询用户
     */
    @Select("SELECT * FROM user WHERE username = #{username} ")
    User findByUsername(@Param("username") String username);

    /**
     * 根据手机号查询用户
     */
    @Select("SELECT * FROM user WHERE phone = #{phone} AND deleteStatus = 0")
    User findByPhone(@Param("phone") String phone);

    /**
     * 根据邮箱查询用户
     */
    @Select("SELECT * FROM user WHERE email = #{email} AND deleteStatus = 0")
    User findByEmail(@Param("email") String email);

    /**
     * 根据openid查询用户
     */
    @Select("SELECT * FROM user WHERE openid = #{openid} AND deleteStatus = 0")
    User findByOpenid(@Param("openid") String openid);

    /**
     * 获取教师列表
     */
    @Select("SELECT * FROM user WHERE role = 1 AND deleteStatus = 0 AND status = 0 ORDER BY createTime DESC")
    List<User> getTeacherList();

    /**
     * 分页获取教师列表
     */
    @Select("SELECT * FROM user WHERE role = 1 AND deleteStatus = 0 AND status = 0 ORDER BY createTime DESC LIMIT #{offset}, #{limit}")
    List<User> getTeacherListPage(@Param("offset") Integer offset, @Param("limit") Integer limit);

    /**
     * 获取教师总数
     */
    @Select("SELECT COUNT(*) FROM user WHERE role = 1 AND deleteStatus = 0 AND status = 0")
    Integer getTeacherCount();

    /**
     * 根据角色获取用户列表
     */
    @Select("SELECT userid, username, nickname, avatar, phone, email, sex as sex, age, place, city, role, status, integral, introduce, createTime, updateTime " +
            "FROM user WHERE role = #{role} AND deleteStatus = 0 ORDER BY createTime DESC")
    List<UserVO> getUsersByRole(@Param("role") Integer role);
} 